<?php

require_once 'common/conn.php';
$usercode = $_GET['usercode'];
if (!isset($usercode)) {
    $usercode = $_POST['usercode'];
}
if (isset($_GET['start'])) {
    $start = $_GET['start'];
} else {
    $start = 0;
}
if (isset($_GET['limit'])) {
    $limit = $_GET['limit'];
}

if (isset($_GET['type'])) {
    $type = $_GET['type']; //1 动画故事 2 互动学习 3 益智游戏 4 音乐
}

$data = array();
if (is_numeric($start) && is_numeric($limit)) {
    $db = new DBC();
    if ($type == 4) {
        $sql = "SELECT p.flashcode,p.type1 AS type,d.downloadTime,MAX(d.usedNumberTimes) AS playedTimes,p.url from t_music p left join t_device_product d on p.flashcode = d.flashcode AND d.usercode = '" . $usercode . "' GROUP BY p.flashcode LIMIT ".$start.",".$limit;
    } else {
//      $sql = "SELECT usercode,flashcode,type,downloadTime FROM t_device_product WHERE usercode = '".$usercode."' ORDER BY downloadTime DESC LIMIT " . $start . "," . $limit;
        $sql = "SELECT p.flashcode,p.type1 AS type,d.downloadTime,MAX(d.usedNumberTimes) AS playedTimes,p.url from t_animation p left join t_device_product d on p.flashcode = d.flashcode AND d.usercode = '" . $usercode . "' GROUP BY p.flashcode LIMIT ".$start.",".$limit;
    }
    $temp = $db->fetchData($sql);
    if ($temp != false) {
        foreach ($temp as $row) {
            if ($row['downloadTime'] != null && $row['downloadTime'] != "null" && $row['downloadTime'] != "") {
                $row['isdownload'] = "1";
            } else {
                $row['isdownload'] = "0";
                $row['downloadTime'] = "";
            }
            if ($row['playedTimes'] != null && $row['playedTimes'] != "null" && $row['playedTimes'] != "") {
                
            } else {
                $row['playedTimes'] = "0";
            }
            if ($row['url'] != null && $row['url'] != "null" && $row['url'] != "") {
                
            } else {
                $row['url'] = "";
            }
            array_push($data, $row);
        }
    }
}
echo json_encode($data);
?>